// pages/detail/detail.js
const {service} = require("../../data/data")
const db = wx.cloud.database();
const shopCollection = db.collection("shop");
const shopclassifyCollection = db.collection("shopclassify");
const shopcartCollection = db.collection("shopcart") 
const _ = db.command
// var choiceid
// var shopNumber = ""
var choiceNumber=1
var order = 1
Page({

    /**
     * 页面的初始数据
     */
    data: {
        service,
        show: false,
        classifyid:1,
        choiceid:1,
        choiceNumber:1,
     
        // databaseNumber,
    },
    gobackhome(){
        wx.switchTab({
          url: '../home/home',
        })
    },
    showPopup() {
        this.setData({ show: true });
      },
    
      onClose() {
        this.setData({ show: false });
      },
    gotocart(){
        wx.switchTab({
          url: '../shop_cart/shop_cart',
        })
    },
    onChange(e){
        choiceNumber = e.detail;
        // console.log(choiceNumber,"--");
        this.setData({
            choiceNumber
        })
    },
   async addToCart(){
       console.log(this.data.Sid,"==========");
        console.log( this.data.classifyid);
        let {data} = await shopclassifyCollection
                    .where(_.and([{C_id:_.eq(this.data.classifyid)},{S_id:_.eq(Number(this.data.Sid))}]))
                    .get()
        let  classifydata =  [...data]
        console.log(this.data.Sid,"==========");
        console.log(data,",,,,,,,,,");
        console.log(classifydata,",,,,,,,,,,,,,");
        let {data:data1} = await shopcartCollection
        .where(_.and([{C_id:_.eq(this.data.classifyid)},{S_id:_.eq(Number(this.data.Sid))}]))
        .get()

        console.log(data1,"----------------");
        if(data1.length==0){
            console.log(classifydata);
            
            await classifydata.forEach(res=>{
            // console.log(res);
            shopcartCollection.add({
            data:{...res,C_Number:this.data.choiceNumber,selected:false,order:order,C_introduce:this.data.shopinformation[0].S_introduce}
            })
            order = order +1;
            
        })
        }else{
            // console.log("..................");
            let {data:data2} = await shopcartCollection
            .where({C_id:_.eq(this.data.classifyid)})
            .get()
            // console.log(data2,"--------------");
            await data2.forEach(res=>{
                this.setData({
                    databaseNumber:res.C_Number,
                    show:false
                })
                
            })
            await shopcartCollection
            .where({C_id:_.eq(this.data.classifyid)})
            .update({
                 data:{
                    C_Number:this.data.choiceNumber+this.data.databaseNumber
                 },
                 success:res=>{
                     console.log(res);
                 }
             })
        }
        // let a = await shopcartCollection.get();
        // console.log(a);
        let {data:data2} = await shopcartCollection.get();
        console.log(data2,"??????????");
        let {total:total1} = await shopcartCollection.count()
        console.log(total1,"llllllllllllll");
                this.setData({
                     total:total1,
                     show:false
                })
        wx.showToast({
            icon:"success",
            title: '加入购物车成功',
            duration: 4000
        })

    },
     buyImmediately(){

    },
   
    choiceclassify(e){
        let { id } = e.currentTarget.dataset;
        console.log(id);
        this.data.shopclassify.forEach(res=>{
            console.log(res.C_id);
            if(res.C_id == id){
                this.setData({
                    classifyid:id,
                    choose:res.C_classify
                })  
            }
        })
        // console.log(classify);
        // this.setData({
        //     choose:res.C_classify
        // })
        console.log(this.data.choose);
        // let arr =[]
        // this.data.shopinformation.forEach(res=>{
        //     console.log(res.S_classification);
        //     arr.push(res.S_classification)
        // })
        // console.log(arr);
        // console.log(this.data.shopinformation);
        // let {shopclassify:S_classification } = this.data.shopinformation
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        let { id } = options;
        console.log(id,"-----------");
        let {data} = await shopCollection
        .where({S_id:_.eq(Number(id))})
        .orderBy("S_price","desc")
        .get()
        let {data:data1} = await shopclassifyCollection
        .where({S_id:_.eq(Number(id))})
        .orderBy("C_price","desc")
        .get()
        // console.log(data);
        console.log(data1);
        this.setData({
            Sid:id,
            shopinformation:data,
            shopclassify:data1
        })
        this.data.shopclassify.forEach(res=>{
            if(res.C_id == this.data.choiceid){
                this.setData({
                    choose:res.C_classify
                })
                // classify.push(res)
            }
        })

        // console.log(this.data.shopinformation);
        let {total} = await shopcartCollection.count()
        this.setData({
            total:total
        })
        // console.log(total); 
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    async onShow() {
        // let {total:total1} = await shopcartCollection.count()
        // console.log(total1,"llllllllllllll");
        //         this.setData({
        //              total:total1,     
        //     })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})